package Thread;

import java.util.Scanner;

//多线程相关知识的练习；
//wait和notify可以进行控制相关线程的执行顺序的相关代码的练习；
public class Demo24 {
    public static  void main(String[] args){
        //进行锁对象的定义：
        Object locker=new Object();
        Thread t1=new Thread(()->{
            synchronized (locker){
                System.out.println("t1在执行wait方法之前；");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1在执行wait方法之后");
            }
        });
        Thread t2=new Thread(()->{
            synchronized (locker){
                System.out.println("t2在执行wait方法之前；");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t2在执行wait方法之后");
            }
        });
        Thread t3=new Thread(()->{
            synchronized (locker){
                System.out.println("t3在执行wait方法之前；");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t3在执行wait方法之后");
            }
        });
        Thread t=new Thread(()->{
            Scanner scanner=new Scanner(System.in);
            System.out.println("请进行输入任意内容唤醒以上线程");
            scanner.next();
            synchronized (locker){
//                locker.notify();//进行唤醒wait方法；
                locker.notifyAll();//进行唤醒以上所有的线程；
            }
        });
        t1.start();
        t2.start();
        t3.start();
        t.start();
    }
}
